package gorm

import (
	_ "github.com/go-sql-driver/mysql"
	"github.com/jinzhu/gorm"
	"github.com/jmoiron/sqlx"
)

type User struct {
	//gorm.Model
	// 使用指针类型，避免零值；
	Id   uint   `gorm:"primary_key;AUTO_INCREMENT"`
	Name string `gorm:"type:varchar(64)"`

	// 驼峰式内部自动转换为 addr_city
	AddrCity string `gorm:"type:varchar(255)"`
}

var db *sqlx.DB

// 初始化方法
func TestGorm() {
	conn, err := gorm.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/golang-go?charset=utf8")
	if err != nil {
		panic(err)
	}
	// 不设置这个的话，gorm会对表明自动加一个后缀s，导致找不到表
	conn.SingularTable(true)
	defer conn.Close()
	conn.Create(&User{Id: 20, Name: "勾起", AddrCity: "上海"})
}
